<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyBatis-Plus 代码生成器</title>
    <style>
        body {
            font-family: 'Inter', sans-serif; /* 使用Inter字体 */
            background-color: #f3f4f6;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            box-sizing: border-box;
        }
        .container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
            border: 1px solid #e5e7eb;
            box-sizing: border-box;
        }
        h2 {
            text-align: center;
            color: #1f2937;
            margin-bottom: 30px;
            font-size: 2rem;
            font-weight: bold;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #374151;
            font-weight: 500;
            font-size: 0.9rem;
        }
        input[type="text"],
        input[type="number"],
        input[type="password"],
        select {
            width: 100%;
            padding: 12px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            box-sizing: border-box;
            font-size: 1rem;
            color: #374151;
            transition: border-color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
        }
        input[type="text"]:focus,
        input[type="number"]:focus,
        input[type="password"]:focus,
        select:focus {
            outline: none;
            border-color: #3b82f6;
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
        }
        .grid-container {
            display: grid;
            grid-template-columns: 1fr;
            gap: 20px;
        }
        @media (min-width: 640px) {
            .grid-container {
                grid-template-columns: 1fr 1fr;
            }
            .grid-span-2 {
                grid-column: span 2;
            }
        }
        button {
            width: 100%;
            padding: 14px;
            background-color: #2563eb;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1.1rem;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
            box-shadow: 0 4px 10px rgba(37, 99, 235, 0.2);
        }
        button:hover {
            background-color: #1d4ed8;
            box-shadow: 0 6px 15px rgba(37, 99, 235, 0.3);
        }
        button:disabled {
            background-color: #93c5fd;
            cursor: not-allowed;
            box-shadow: none;
        }
        .message {
            margin-top: 20px;
            text-align: center;
            font-size: 0.9rem;
            padding: 10px;
            border-radius: 8px;
        }
        .message.success {
            background-color: #d1fae5;
            color: #065f46;
            border: 1px solid #34d399;
        }
        .message.error {
            background-color: #fee2e2;
            color: #991b1b;
            border: 1px solid #ef4444;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>MyBatis-Plus 代码生成器</h2>

    <form id="generatorForm">
        <div class="form-group">
            <label for="dbType">数据库类型</label>
            <select id="dbType">
                <option value="MYSQL">MySQL</option>
                <option value="DM">达梦数据库 (DM)</option>
                <option value="KINGBASEES">人大金仓 (KingbaseES)</option>
                <option value="POSTGRESQL">PostgreSQL</option>
            </select>
        </div>

        <div class="grid-container">
            <div class="form-group">
                <label for="host">主机</label>
                <input type="text" id="host" value="localhost" required>
            </div>
            <div class="form-group">
                <label for="port">端口</label>
                <input type="number" id="port" value="" required>
            </div>
            <div class="form-group grid-span-2">
                <label for="database">数据库名 / Schema</label>
                <input type="text" id="database" required>
            </div>
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password">
            </div>
        </div>

        <div class="grid-container">
            <div class="form-group">
                <label for="author">作者</label>
                <input type="text" id="author" value="YourName">
            </div>
            <div class="form-group">
                <label for="parentPackage">父包名</label>
                <input type="text" id="parentPackage" value="com.example.codegen" required>
            </div>
        </div>

        <div class="form-group">
            <label for="tableNames">指定表名 (逗号分隔，留空则生成所有表)</label>
            <input type="text" id="tableNames" placeholder="例如: user, product, order_item">
        </div>

        <button type="submit" id="submitBtn">生成代码并下载</button>

        <div id="message" class="message" style="display: none;"></div>
    </form>
</div>

<script>
    const dbTypeSelect = document.getElementById('dbType');
    const portInput = document.getElementById('port');
    const generatorForm = document.getElementById('generatorForm');
    const submitBtn = document.getElementById('submitBtn');
    const messageDiv = document.getElementById('message');

    // 根据数据库类型设置默认端口
    function setDefaultPort() {
        const dbType = dbTypeSelect.value;
        switch (dbType) {
            case 'MYSQL':
                portInput.value = '3306';
                break;
            case 'DM':
                portInput.value = '5236';
                break;
            case 'KINGBASEES':
                portInput.value = '54321'; // KingbaseES 默认端口
                break;
            case 'POSTGRESQL':
                portInput.value = '5432';
                break;
            default:
                portInput.value = '';
        }
    }

    // 初始化时设置一次默认端口
    document.addEventListener('DOMContentLoaded', setDefaultPort);
    // 监听数据库类型变化，更新端口
    dbTypeSelect.addEventListener('change', setDefaultPort);

    // 处理表单提交
    generatorForm.addEventListener('submit', async (e) => {
        e.preventDefault(); // 阻止表单默认提交行为

        submitBtn.disabled = true;
        submitBtn.textContent = '生成中...';
        messageDiv.style.display = 'none';
        messageDiv.className = 'message'; // 重置样式

        const tableNamesValue = document.getElementById('tableNames').value;
        const tableNamesArray = tableNamesValue
            .split(',')
            .map(t => t.trim())
            .filter(t => t.length > 0);

        const requestBody = {
            dbType: dbTypeSelect.value,
            host: document.getElementById('host').value,
            port: parseInt(portInput.value),
            database: document.getElementById('database').value,
            username: document.getElementById('username').value,
            password: document.getElementById('password').value,
            author: document.getElementById('author').value,
            parentPackage: document.getElementById('parentPackage').value,
            tableNames: tableNamesArray
        };

        try {
            const response = await fetch('/generate', { // 后端接口地址
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(requestBody),
            });

            if (response.ok) {
                const blob = await response.blob();
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = 'generated_code.zip'; // 下载文件名
                document.body.appendChild(a);
                a.click();
                a.remove();
                window.URL.revokeObjectURL(url);
                showMessage('代码已成功生成并下载！', 'success');
            } else {
                const errorText = await response.text();
                showMessage(`生成失败: ${errorText || response.statusText}`, 'error');
            }
        } catch (error) {
            showMessage(`请求出错: ${error.message}`, 'error');
        } finally {
            submitBtn.disabled = false;
            submitBtn.textContent = '生成代码并下载';
        }
    });

    // 显示消息函数
    function showMessage(msg, type) {
        messageDiv.textContent = msg;
        messageDiv.className = `message ${type}`;
        messageDiv.style.display = 'block';
    }
</script>
</body>
</html>
